package com.dsbj.icardservice.service;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.icardservice.entity.Employee;
import com.dsbj.icardservice.utils.Constants;
import com.dsbj.icardservice.utils.strprocess.StrUtil;
import com.dsbj.icardservice.webserviceclient.dsbjpmc.AllowUserResponseMessage;
import com.dsbj.icardservice.webserviceclient.dsbjpmc.JWSService;
import com.dsbj.icardservice.webserviceclient.dsbjpmc.WxUser;
import com.dsbj.icardservice.webserviceclient.dsbjpmc.WxUserResponseMessage;
import com.dsbj.starter.redis.utils.RedisHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WxUserService {

	@Autowired
	private JWSService jwsService;

	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private RedisHelper redisHelper;
	

	public ResponseMessage wxBindUser(Integer type, String openId, String username, String password, String employeeNo, String phoneNo, String vCode) {
		if(type.equals(0)){
			return wxBindICardUser(openId,username,password);
		}else{
			return wxBindEmployee(openId,employeeNo,phoneNo,vCode);
		}
	}

	private ResponseMessage wxBindEmployee(String openId, String employeeNo, String phoneNo, String vCode) {
		
		/**
		 * 验证客户填入的验证码(vCode)是否正确
		 */
		String vCodeReal=(String)redisHelper.get(Constants.WX_BIND_VCODE_PREFIX +phoneNo);
		if(StrUtil.isEmpty(vCodeReal)||!vCodeReal.equals(vCode)){
			return ResponseMessage.error("验证码错误，请重新获取验证码!");
		}
		
		//根据手机号查询员工,检查员工工号和填写的工号是否一致
		Employee employee = (Employee) employeeService.getEmployeeByPhoneNum(phoneNo).getData();
		if(employee==null){
			return ResponseMessage.error("用户手机号不存在");
		}
		if(!StringUtils.equals(employeeNo,employee.getEmployeeno())){
			return ResponseMessage.error("工号不一致");
		}

		AllowUserResponseMessage msg = jwsService.getUserByEmployeeno(employeeNo);
		if(!msg.isSuccess()){
			return ResponseMessage.error(msg.getMessage());
		}
		if(msg.getAllowUser()!=null){
			//校验公司编号是否一致
			if(!StringUtils.equals(employee.getCompanyno(),msg.getAllowUser().getCompanyno())){
				return ResponseMessage.error("公司编号不一致,请联系后台管理人员");
			}
		}
		msg = jwsService.wxBindUser(openId,msg.getAllowUser().getEmployeeNo());
		if(msg.isSuccess()){
			return ResponseMessage.ok();
		}else{
			return ResponseMessage.error(msg.getMessage());
		}
	}

	private ResponseMessage wxBindICardUser(String openId, String username, String password) {
		AllowUserResponseMessage msg = jwsService.getUserByUsernameAndPassWord(username,password);
		if(!msg.isSuccess()||msg.getAllowUser()==null||StringUtils.isBlank(msg.getAllowUser().getEmployeeNo())){
			return ResponseMessage.error(msg.getMessage());
		}
		//通过工号查询员工,校验companyno是否一致
		Employee employee = employeeService.getEmployeeByNo(msg.getAllowUser().getEmployeeNo());
		if(employee!=null&&!employee.getCompanyno().equals(msg.getAllowUser().getCompanyno())){
			return ResponseMessage.error("公司编号不一致,请联系后台管理人员");
		}

		msg = jwsService.wxBindUser(openId,msg.getAllowUser().getEmployeeNo());
		if(msg.isSuccess()){
			return ResponseMessage.ok();
		}else{
			return ResponseMessage.error(msg.getMessage());
		}
	}

	public WxUserResponseMessage getWxUserByOpenId(String openId) {
		WxUserResponseMessage msg = jwsService.getWxUserByOpenId(openId);

		if(!msg.isSuccess()||msg.getWxUser()==null){
			return msg;
		}

		///根据工号查普通员工
		WxUser wxUser = msg.getWxUser();
		Employee employee = employeeService.getEmployeeByNo(wxUser.getEmployeeNo());

		if(wxUser.getLevel().equals(0)){
			//0表示不是一卡通用户，需要判断是否是普通用户
			if(employee==null){
				msg.setSuccess(false);
				msg.setMessage("用户不存在");
				return msg;
			}else{
				wxUser.setDeptno(employee.getDeptno());
				wxUser.setCompanyno(employee.getCompanyno());
				wxUser.setName(employee.getEmployeename());
				wxUser.setSex(employee.getSex());
			}
		}else{
			if(employee!=null){
				wxUser.setLevel(2);
				wxUser.setDeptno(employee.getDeptno());
			}
		}

		return msg;
	}
	
	
	
	
	
	
}